// JavaScript Document

var images = {

    init: function() {
        this.bindHeaderLinks();
        this.bindImageLinks();
    },
    
    bindHeaderLinks: function() {
        $("#add-link").click(function(e) {
            e.preventDefault();
            
            var url = $(this).attr("href");
            
            $.get(url, function(data) {
                $p.showDialog({
                    title: "Upload New Image",
                    content: data,
                    okButtonText: "Upload Selected",
                    okButtonAction: function() {
                        $("#upload_status").css({color: 'red'}).html("Uploading Images To Server. Please Wait ...");
                        
                        var page = $("#img_upload").attr("action");
                        
                        $p.postMultiPartForm(page, "img_upload", null, function(data) {
                            images.updateList(data);
                            $p.hideDialog();
                        });
                        
                        return false;
                    }
                    
                }, function() {
                    $("#category").val("cms");
                    images.configureUploadField();
                });
            });
        });
        
        $("#checkall").change(function(e) {
            if( this.checked ) {
                $(".image_list input[type=checkbox]").attr("checked", "checked");
                $(".image_list li").addClass("selected");
            }
            
            else {
                $(".image_list input[type=checkbox]").removeAttr("checked");
                $(".image_list li").removeClass("selected");
            }
        });
        
        $("#del-selected-link").click(function(e) {
            e.preventDefault();
            
            if( !confirm("Delete Selected Images From System?") ) {
                return;
            }
            
            var checked = [];
            $(".image_list input[type=checkbox]").each(function() {
                if( this.checked ) {
                    checked.push( this.value );
                
                    var $this = $(this);
                    $this.parents("li").fadeOut('slow', function() {
                        $this.parents("li").remove();
                    });
                }
            });
            
            var query = "ids=" + checked;
            $.post( $(this).attr('href'), query);
        });
        
        $(".image_list .img_name").dblclick(function() {
            $(this).attr("contenteditable", true).addClass("editable");
            $(this).data("value", $(this).text());
            
        }).blur(function() {
            var $this = $(this);
            $this.removeAttr("contenteditable").removeClass("editable");
            
            if( $this.data("value") != $this.text() ) {
                var url = $this.data('url');
                var query = "image_name=" + $this.text() + "&id=" + $this.parents('li').data('id');
                
                $.post(url, query, function() {});
            }
            
        }).keydown(function(e) {
            if( e.keyCode == 13 ) {
                e.preventDefault();
                $(this).blur();
            }
        });
    },
    
    bindImageLinks: function() {
        $(".delete_link").click(function(e) {
            e.preventDefault();
			
            if( !confirm('Are you sure you want to delete this image from the system?') ) {
                return;
            }
			
            var $this = $(this);
			
            $.get( $(this).attr('href'), function(data) {
                $this.parents("li").fadeOut('slow', function() {
                    $this.parents("li").remove();
                });	
            });
        });
        
        
        $(".image_list li input[type=checkbox]").change(function() {
            if( this.checked ) {
                $(this).parents("li").addClass("selected");
            }
            
            else {
                $(this).parents("li").removeClass("selected");
                $("#checkall").removeAttr("checked");
            }
        });
        
        $(".image_list li img").click(function() {
            var img = "<img src='" + $(this).data('image-url') + "' />";
            
            $p.showMessage({
                title: "Image Preview",
                content: img
            });
        });
    },
    
    configureUploadField: function() {
        
        var self = this;
        self.filesSelected = false;
        
        $("#image_fs").change(function(evt) {
            if( evt.target.files != null ) {
                    
                var total_size = 0;
                    
                var $ul = $("<ol />").css('list-style-type', 'decimal');
                $.each(evt.target.files, function(k,v) {
                    var size = (v.size / 1024).toFixed(2) + " KB";
                    total_size += v.size;
                            
                    if( parseFloat(size) > 1024 ) {
                        size = (parseFloat(size) / 1024).toFixed(2) + " MB";
                    }
                            
                    $ul.append( $("<li />").html(v.name + " (" + size + ")") );
                });
                    
                var max_size_raw = $("#max_size_value").html();
                var max_size_value = parseInt(max_size_raw.replace("M", ""));
                    
                if( total_size > (max_size_value * 1024 * 1024) ) {
                    $("#file_list").html("").append("<span style='color: #900; font-weight: bold;'>Error! Max Upload Size of " + max_size_raw + " Exceeded</span>");
                    $("#up_save_btn").attr('disabled','');
                }
                    
                else {
                    $("#file_list").html("<b>Files To Upload</b> <br /><br />").append( $ul ); 
                    $("#up_save_btn").removeAttr('disabled');
                }
            }
                
            // IE and other boys
            else {
                $("#up_save_btn").removeAttr('disabled');
            }
                
            $p.centerDialog();
            self.filesSelected = true;
        });
            
        $("#resize").change(function() {
            if( this.checked ) {
                $("#resize_options label").css("color", "#000");
                $("#resize_options input").removeAttr("disabled");
            }
            
            else {
                $("#resize_options label").css("color", "#ccc");
                $("#resize_options input").attr("disabled","disabled");
            }
        });
    },
    
    updateList: function(img_data) {
        /*if( JSON ) {
            var obj = JSON.parse(img_data);
            
            $.each(obj, function(key, value) {
                var div = $("<div />").addClass("img_box").append( $("<img />").attr({
                    src: value.ImageFile
                }));
                
                var options = $("<div />").addClass("option");
                    options.append( $("<a />").addClass("edit_link").);
                var li = $("<li />");
                    li.append(div);
            });
        }
        
        else {*/
            window.history.go(0);
        //}
    }
};

$(document).ready(function() {
    images.init();
});